<?php
namespace app\admin\controller;

use think\Db;

class Home extends Base{

    public function config(){
        $list = Db::table('bze_config')
            ->where(['is_show'=>1,'location'=>1])
            ->order('sort')
            ->select();
        return $this->fetch('', [
            'list'  =>  $list,
            'list2'  =>  json_encode($list),
        ]);
        return $this->fetch();
    }

    /**
     * @description : [首页]
     * @return mixed
     */
    public function index(){
        return $this->fetch();
    }

    /**
     * @description : [时装发布]
     */
    public function fashion(){
        if(!request()->isPost()){
            $list = model('config')
                ->where(['group_key'=>'FASHION'])
                ->select();
            return $this->fetch('', [
                'list' => $list,
            ]);
        }
    }

    /**
     * @description : [时装发布 - 添加/编辑]
     * @return mixed
     */
    public function addFashion(){
        if(request()->isPost()){
            $postData = input('post.');
            model('Fashion')->add($postData);
            $this->success('添加成功', 'home/fashionList');
        }else{
            return $this->fetch();
        }
    }

    /**
     * @description : [时装发布列表]
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function fashionList(){
        $list = model('Fashion')->order('sort')->select();
        return $this->fetch('', [
            'list' => $list,
        ]);
    }

    /**
     * @description : [时装发布 - 删除]
     */
    public function deleteFashion(){
        $postData = input('post.');
        $id = $postData['id'];
        $res = model('Fashion')->where(['id'=>$id])->delete();
        if(false !== $res){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    /**
     * @description : [品牌链接]
     */
    public function brandLink(){
        if(!request()->isPost()){
            $list = model('config')
                ->where(['group_key'=>'HOME_BRAND'])
                ->select();
            return $this->fetch('', [
                'list' => $list,
            ]);
        }
    }

    /**
     * @description : [品牌链接]
     */
    public function bottomImg(){
        if(!request()->isPost()){
            $list = model('IndexBottom')->select();
            return $this->fetch('', [
                'list' => $list,
            ]);
        }
    }

    public function addBottomImg(){
        if(!request()->isPost()){
            return $this->fetch();
        }
        $postData = input('post.');
        try{
            model('IndexBottom')->save($postData);
        }catch (\Exception $e){
            $this->error('添加失败。'.$e->getMessage());
        }
        $this->success('添加成功', 'home/bottomimg');
    }

    public function deleteBottomImg(){
        $id = input('param.id');
        model('IndexBottom')->where(['id'=>$id])->delete();
        return show(0, '删除成功');
    }
}
